<template>
  <div class="home">
      
        <el-container>
            <!-- 头部 -->
            <el-header>
                 <span @click="toggle">折叠展开</span>
                 <span @click="all"> 全屏</span>
            </el-header>
            <el-container>
                <!-- 侧边 -->
                <el-aside :width="width">
                    <el-menu default-active="1-4-1" class="el-menu-vertical-demo" router :collapse="isCollapse">
                            <el-menu-item index="/list">
                                <i class="el-icon-document"></i>
                                <span slot="title">基础表格</span>
                            </el-menu-item>
                            <el-menu-item index="/tabs">
                                <i class="el-icon-setting"></i>
                                <span slot="title">tab选项卡</span>
                            </el-menu-item>
                </el-menu>
                </el-aside>
                <!-- 内容 -->
                <el-main>
               
                    <el-tag
                        :key="tag.path"
                        v-for="tag,index in routeList"
                        closable
                        @click="handleGo(tag.path)"
                        @close="handleClose(index)">
                        {{tag.title}}
                        </el-tag>
                        <!-- 下拉 -->
                        <el-dropdown split-button type="primary" @command="handleCommand">
                                标签选项
                                <el-dropdown-menu slot="dropdown">
                                    <el-dropdown-item command="all">关闭所有</el-dropdown-item>
                                    <el-dropdown-item command="other">关闭其他</el-dropdown-item>
                                   
                                </el-dropdown-menu>
                        </el-dropdown>
                        <!-- 坑 -->
                        <router-view />
                </el-main>
            </el-container>
        </el-container>
  </div>
</template>

<script>
// @ is an alias to /src
// 全屏的插件 screenfull
import screenfull from 'screenfull';

export default {
  name: 'HomeView',
  data() {
    return {
        isCollapse:false,//展开折叠的状态
        width:'200px',
        routeList:[],//存放路由信息
    }
  },
  methods: {
    toggle(){
       if(this.isCollapse){
        this.width = '200px'
        this.isCollapse = false
       }else{
        this.width = '60px'
        this.isCollapse = true
       }
    },
    // 全屏切换
    all(){
      if (screenfull.isEnabled) { //判断是否支持全屏
            screenfull.request(); //切换全屏
        }
    },
    // 点击关闭tag
    handleClose(index){
         this.routeList.splice(index,1)
    },
    // 点击tag跳转
    handleGo(path){
        // 如果当前的路由和我要跳转的路由相同   就return 
       if(path == this.$route.path) return 
       this.$router.push(path)
    },
    handleCommand(val){
        console.log(val);
        if(val == 'all'){
            // 关闭所有
            this.routeList = []
        }else{
            // 留下当前页面的，关闭其他页面的 利用 this.$route路由信息对象
         this.routeList = [{
            title:this.$route.meta.title,
            path:this.$route.path
         }]
        }

    }
  },
  watch:{
    $route:{
        handler(newVal){
           console.log(newVal);
         let exits = this.routeList.find(item=>item.path == newVal.path)
         if(!exits){
            this.routeList.push({
                title:newVal.meta.title,
                path:newVal.path
            })
         }
         console.log(this.routeList);
        },
        immediate:true
    }
  },
  components: {
    
  }
}
</script>
<style lang="scss" scoped>
  .el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }
  
  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }
</style>

